/**
 * 
 */
package com.tvshows.front.view.menu;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.tvshows.front.listener.menu.MenuListener;
import com.tvshows.front.ui.FrontUI;
import com.tvshows.front.ui.menu.MenuUI;

/**
 * @author tMidy
 * <p>
 * Implementation of {@link MenuUI}
 * </p>
 */
public class MenuView
    implements MenuUI
{

    private MenuItem optionsItem;

    private MenuItem quitterItem;

    private MenuItem exportXmlItem;

    private FrontUI mainView;

    private MenuItem importXmlItem;

    private List<InputStream> imagesList;

    /**
     * Configures the frame menus
     */
    @Override
    public void initMenus( FrontUI view )
    {
        MenuListener menuListener = new MenuListener( this );
        this.mainView = view;

        /* Configures image icons */
        configureImages();
        view.getShell().setImage( new Image( view.getShell().getDisplay(), imagesList.get( 5 ) ) );

        Menu menu = new Menu( view.getShell(), SWT.BAR | SWT.BORDER );

        /* Configures menu items */
        MenuItem file = new MenuItem( menu, SWT.CASCADE );

        file.setText( "Fichier" );
        Menu projectMenu = new Menu( view.getShell(), SWT.DROP_DOWN );
        file.setMenu( projectMenu );
        Menu menuBar = new Menu( view.getShell(), SWT.BAR );
        MenuItem fileMenuHeader = new MenuItem( menuBar, SWT.CASCADE );
        fileMenuHeader.setText( "Fichier" );

        Menu fileMenu = new Menu( view.getShell(), SWT.DROP_DOWN );
        fileMenuHeader.setMenu( fileMenu );
        quitterItem = new MenuItem( fileMenu, SWT.PUSH );
        quitterItem.setText( "Quitter" );
        quitterItem.addListener( SWT.Selection, menuListener );
        quitterItem.setImage( new Image( view.getShell().getDisplay(), imagesList.get( 4 ) ) );

        MenuItem helpMenuHeader = new MenuItem( menuBar, SWT.CASCADE );
        helpMenuHeader.setText( "Options" );
        Menu helpMenu = new Menu( view.getShell(), SWT.DROP_DOWN );
        helpMenuHeader.setMenu( helpMenu );

        optionsItem = new MenuItem( helpMenu, SWT.CASCADE );
        optionsItem.setText( "Configuration" );
        optionsItem.addListener( SWT.Selection, menuListener );
        optionsItem.setImage( new Image( view.getShell().getDisplay(), imagesList.get( 2 ) ) );

        exportXmlItem = new MenuItem( helpMenu, SWT.CASCADE );
        exportXmlItem.setText( "Exporter la liste des favoris..." );
        exportXmlItem.addListener( SWT.Selection, menuListener );
        exportXmlItem.setImage( new Image( view.getShell().getDisplay(), imagesList.get( 1 ) ) );

        importXmlItem = new MenuItem( helpMenu, SWT.CASCADE );
        importXmlItem.setText( "importer des favoris..." );
        importXmlItem.addListener( SWT.Selection, menuListener );
        importXmlItem.setImage( new Image( view.getShell().getDisplay(), imagesList.get( 0 ) ) );

        view.getShell().setMenuBar( menuBar );
    }

    /**
     * Configures the images for the menu
     */
    private void configureImages()
    {
        imagesList = new ArrayList<InputStream>();

        imagesList.add( getClass().getResourceAsStream( "/images/import.jpg" ) );
        imagesList.add( getClass().getResourceAsStream( "/images/export.jpg" ) );
        imagesList.add( getClass().getResourceAsStream( "/images/options.png" ) );
        imagesList.add( getClass().getResourceAsStream( "/images/log.png" ) );
        imagesList.add( getClass().getResourceAsStream( "/images/exit.png" ) );
        imagesList.add( getClass().getResourceAsStream( "/images/shows.jpg" ) );
    }

    /**
     * @return the new Item
     */
    @Override
    public MenuItem getQuitterItem()
    {
        return quitterItem;
    }

    /**
     * @return the optionsItem
     */
    @Override
    public MenuItem getOptionsItem()
    {
        return optionsItem;
    }

    /**
     * @return the optionsItem
     */
    @Override
    public MenuItem getExportXmlItem()
    {
        return exportXmlItem;
    }

    /**
     * @see com.tvshows.front.ui.menu.MenuUI#getImportXmlItem()
     */
    @Override
    public MenuItem getImportXmlItem()
    {
        return importXmlItem;
    }

    /**
     * @see com.tvshows.front.ui.menu.MenuUI#showSuccess()
     */
    @Override
    public void showSuccess()
    {
        MessageBox box = new MessageBox( mainView.getShell(), SWT.ICON_QUESTION );
        box.setMessage( "Export/Import réalisé avec succés !" );
        box.open();
    }

    /**
     * @see com.tvshows.front.ui.menu.MenuUI#showFailure()
     */
    @Override
    public void showFailure()
    {
        MessageBox box = new MessageBox( mainView.getShell(), SWT.ICON_QUESTION );
        box.setMessage( "Export/Import a rencontré une erreur, consultez le log !" );
        box.open();
    }

    /**
     * @see com.tvshows.front.ui.menu.MenuUI#getMainView()
     */
    @Override
    public FrontUI getMainView()
    {
        return this.mainView;
    }

    /**
     * @see com.tvshows.front.ui.FrontUI#initFormData()
     */
    @Override
    public void initFormData()
    {
        // Not implemented

    }

    /**
     * @see com.tvshows.front.ui.FrontUI#initComposites()
     */
    @Override
    public void initComposites()
    {
        // Not implemented

    }

    /**
     * @see com.tvshows.front.ui.FrontUI#getShell()
     */
    @Override
    public Shell getShell()
    {
        return mainView.getShell();
    }

}
